public class Main {
    public long distributeCandies(int n, int limit) {
        long count = 0;

        for(int i = 0;i <= Math.min(limit,n);i++){
            int rem = n - i;
            if(rem > 2*limit){
                continue;
            }
            int bMin = Math.max(0,rem - limit);
            int bMax = Math.min(limit,rem);
            count += bMax - bMin + 1;
        }
        return count;
    }
}